﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.Windows.Forms;

using System.Runtime.Remoting;

namespace Srvtools
{
    public partial class APServerConfigDialog : Form
    {
        public bool AutoConnect
        {
            get
            {
                return this.cbxAutoConnect.Checked;
            }
            set
            {
                this.cbxAutoConnect.Checked = value;
            }
        }

        public string ServerIP
        {
            get
            {
                return this.txtServerIp.Text;
            }
            set
            {
                this.txtServerIp.Text = value;
            }
        }

        private bool OkClicked = false;

        public APServerConfigDialog()
        {
            InitializeComponent();
        }

        private void APServerConfigDialog_Load(object sender, EventArgs e)
        {
            WellKnownClientTypeEntry[] entries = RemotingConfiguration.GetRegisteredWellKnownClientTypes();
            foreach (WellKnownClientTypeEntry entry in entries)
            {
                if (entry.TypeName == "EEPRemoteModule")
                {
                    this.btnConnect.Enabled = false;
                    break;
                }
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            string ipAddress = this.txtServerIp.Text;
            bool isSuccessful = ConnectToApServer(ipAddress);

            if (isSuccessful)
            {
                this.btnConnect.Enabled = false;
                RemotingConfiguration.RegisterWellKnownClientType(typeof(EEPRemoteModule),
                    string.Format("http://{0}:8989/InfoRemoteModule.rem", ipAddress));
                MessageBox.Show("Connection to " + ipAddress + " is successful!");
            }
        }

        internal static bool ConnectToApServer(string ipAddress)
        {
            bool canConnectToRemoteServer = true;
            IPHostEntry ipHostEntry = null;

            try
            {
                if (ipAddress.IndexOf('.') != -1 && ipAddress.Split('.').GetLength(0) != 4)
                {
                    canConnectToRemoteServer = false;
                }
                else
                {
                    ipHostEntry = Dns.GetHostEntry(ipAddress);
                }
            }
            catch
            {
                canConnectToRemoteServer = false;
            }

            bool isRealIpAddress = (ipAddress.Split('.').GetLength(0) == 4) ? true : false;

            //marked by lily 2006/4/18 
            //if (isRealIpAddress && ipHostEntry != null && ipHostEntry.HostName == ipAddress)
            //{
            //    canConnectToRemoteServer = false;
            //}
            //marked by lily 2006/4/18 
                            
            if (!canConnectToRemoteServer)
            {
                MessageBox.Show("Can not connect to " + ipAddress);
                return false;
            }

            string strServiceInfo = "";

            try
            {
                object obj = RemotingServices.Connect(typeof(LoginService),
                    string.Format("http://{0}:8989/Srvtools.rem", ipAddress));

                obj.ToString();
            }
            catch
            {
                strServiceInfo += "Can not find Login Service on " + ipAddress + "\n\r";
            }

            try
            {
                object obj = RemotingServices.Connect(typeof(EEPRemoteModule),
                    string.Format("http://{0}:8989/InfoRemoteModule.rem", ipAddress));

                obj.ToString();
            }
            catch
            {
                strServiceInfo += "Can not find EEPRemoteModule Service on " + ipAddress + "\n\r";
            }

            if (strServiceInfo != null && strServiceInfo.Trim() != "")
            {
                MessageBox.Show(strServiceInfo);
                return false;
            }

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            OkClicked = true;
        }

        private void APServerConfigDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (OkClicked)
            {
                // Data Validation 
                string errString = "";
                // Ip Address
                bool ipError = false;
                string[] ipSlice = this.txtServerIp.Text.Split('.');
                if (ipSlice.GetLength(0) != 4)
                {
                    ipError = true;
                }
                else
                {
                    for (int i = 0; i < ipSlice.GetLength(0); ++i)
                    {
                        if (ipSlice[i].Length > 0 && ipSlice[i].Length < 4)
                        {
                            try
                            {
                                int num = int.Parse(ipSlice[i]);
                                if (num < 0 || num > 255)
                                {
                                    ipError = true;
                                    break;
                                }
                            }
                            catch
                            {
                                ipError = true;
                                break;
                            }
                        }
                        else
                        {
                            ipError = true;
                            break;
                        }
                    }
                }

                if (ipError)
                {
                    errString += "Server Ip Address is not valid.\n\r";
                }

                if (errString != "")
                {
                    e.Cancel = true;
                    OkClicked = false;
                    MessageBox.Show(errString);
                }
            }
        }
    }
}